package cn.vcjf.weapp.api.user.controller;


import cn.vcjf.weapp.api.active.service.GoodsActiveRecordService;
import cn.vcjf.weapp.api.user.entity.User;
import cn.vcjf.weapp.api.user.service.UserService;
import cn.vcjf.weapp.common.sms.SendMsgService;
import cn.vcjf.weapp.common.util.CacheUtils;
import cn.vcjf.weapp.common.web.TableResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-09-25
 */
@Controller
@RequestMapping("/user/user")
public class UserController {
	@Resource
	private UserService userService;
	@Resource
	private SendMsgService sendMsgService;

	@RequestMapping("/regist")
	@ResponseBody
	public TableResult addUser(User user, String code, String isUpdate) {

		String userPhone = user.getPhone();

		if (StringUtils.isNotBlank(userPhone)) {
			EntityWrapper<User> wrapper = user.getQueryWrapper();
			wrapper.eq("phone", userPhone);
			User user1 = userService.selectOne(wrapper);
			//修改信息
			if (StringUtils.isNotBlank(isUpdate)) {
				if (user1 == null) {
					return TableResult.ErrorResult("该手机号不存在!");
				}
				//注册信息
			} else {
				if (user1 != null) {
					return TableResult.ErrorResult("该手机号不存在!");
				}
			}
		} else {
			return TableResult.ErrorResult("手机号不能为空!");
		}
		TableResult result = verifySms(userPhone, code);
		if (result.ERROR()) {
			return result;
		}
		user.preInsert();
		if (StringUtils.isBlank(isUpdate)) {
			user.setRegistTime(new Date());
		}
		boolean b = userService.updateById(user);
		return TableResult.Result(b);
	}
	//发送注册短信验证码
	@RequestMapping("/sendSms")
	@ResponseBody
	public TableResult sendSms(String phone) {
		/*Object smsCodeCache = CacheUtils.getInstance().get("smsCodeCache", phone);
		if (smsCodeCache != null ) {
			return TableResult.ErrorResult("验证码");
		}*/
		EntityWrapper<User> wrapper = new User().getQueryWrapper();

		wrapper.eq("phone", phone);

//		User user = userService.selectOne(wrapper);
//		if(user != null){
//			return TableResult.ErrorResult("该手机号已经被注册,请更换手机号");
//		}
		TableResult result = sendMsgService.sendCustomCodeCache(phone, "smsCodeCache");
		return result;
	}
	//校验发送注册短信验证码
	@RequestMapping("/verifySms")
	@ResponseBody
	public TableResult verifySms(String phone, String code) {
		Object smsCodeCache = CacheUtils.get("smsCodeCache", phone);
		if (smsCodeCache != null && smsCodeCache.equals(code)) {
			return TableResult.OKResult();
		}
		return TableResult.ErrorResult("验证码错误!");
	}

	@RequestMapping("/updateUser")
	@ResponseBody
	public TableResult updateUser(User user,String code) {
		String userPhone = user.getPhone();
		if (StringUtils.isNotBlank(userPhone)) {
			TableResult result = verifySms(userPhone, code);
			if (result.ERROR()) {
				return result;
			}
		}else {
			return TableResult.ErrorResult("手机号不能为空!");
		}

		user.preUpdate();
		boolean b = userService.updateById(user);
		return TableResult.Result(b);
	}

	@RequestMapping("/getUserInfo")
	@ResponseBody
	public TableResult getUserInfo(User user) {

		EntityWrapper<User> wrapper = user.getQueryWrapper();

		User result = userService.selectOne(wrapper);

		return TableResult.OKResult(result);
	}

	@RequestMapping("/findUserList")
	@ResponseBody
	public TableResult findUserList(User user) {

		EntityWrapper<User> wrapper = user.getQueryWrapper();

		Page<User> page = user.getPageInfo();

		page = userService.selectPage(page, wrapper);

		return TableResult.OKResult(page);

	}

	@Resource
	private GoodsActiveRecordService goodsActiveRecordService;

	@RequestMapping("/findUserMessageList")
	@ResponseBody
	public TableResult findUserMessageList(User user) {

		Page<User> page = user.getPageInfo();

		page = userService.findUserMessageList(page, user);

		return TableResult.OKResult(page);

	}

}
